package com.sky.controller.user;

import com.sky.Service.AddressBookService;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@Api(tags = "用户地址簿管理")
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 获取用户地址簿列表
     *
     * @return
     */
    @ApiOperation("获取用户地址簿列表")
    @GetMapping("/list")
    public Result<List<AddressBook>> getAddressBookLists() {
        //获取当前用户id
        Long currentUserId = getCurrentUserId();
        //构造redis中的缓存key
        String key = "addressBookLists_" + currentUserId;
        //查看redis中是否有地址簿数据
        List<AddressBook> addressBookLists = (List<AddressBook>) redisTemplate.opsForValue().get(key);
        //如果存在，直接返回，无需查询数据库
        if (addressBookLists != null && addressBookLists.size() > 0) {
            return Result.success(addressBookLists);
        }
        //如果不存在，则查询数据库，并将查询到的结果存入到redis中

        addressBookLists = addressBookService.getAddressBookLists();
        //log.info("获取用户地址簿列表成功：{}", addressBookLists);
        redisTemplate.opsForValue().set(key, addressBookLists);
        return Result.success(addressBookLists);
    }

    @ApiOperation("添加地址信息")
    @PostMapping
    public Result<String> addAddressBook(@RequestBody AddressBook addressBook) {
        System.out.println("addAddressBook" + addressBook);
        addressBookService.addAddressBook(addressBook);
        //清除缓存
        String key="addressBookLists_*";
        clearCache(key);
        return Result.success("地址信息添加成功！");
    }


    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result<String> setDefaultAddressBook(@RequestBody AddressBook addressBook) {
        System.out.println("AddressBook:" + addressBook);
        addressBookService.setDefaultAddressBook(addressBook.getId());
        //清除缓存
        String key="addressBookLists_*";
        clearCache(key);
        return Result.success("默认地址设置成功！");
    }

    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> selectDefaultAddressBook() {
        log.info("查询默认地址...");
        AddressBook addressBook = addressBookService.selectDefaultAddressBook();
        return Result.success(addressBook);
    }

    @ApiOperation("根据id查询地址信息")
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressBookInfoById(@PathVariable Long id) {
        log.info("查询地址簿信息...");
        AddressBook addressBookInfo = addressBookService.getAddressBookInfoById(id);
        return Result.success(addressBookInfo);

    }

    @ApiOperation("修改地址信息")
    @PutMapping
    public Result<String> modifyAddressBook(@RequestBody AddressBook addressBook) {
        addressBookService.modifyAddressBook(addressBook);
        //清除缓存
        String key="addressBookLists_*";
        clearCache(key);
        return Result.success("地址信息修改成功");
    }

    @DeleteMapping
    public Result<String> deleteAddressBookById(Long id) {
        System.out.println("id:" + id);
        addressBookService.deleteAddressBookById(id);
        //清除缓存
        String key="addressBookLists_*";
        clearCache(key);
        return Result.success("地址信息删除成功");
    }

    public Long getCurrentUserId() {
        return BaseContext.getCurrentUserId();
    }

    /**
     * 清除缓存
     */
    private void clearCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}


